package com.recSys.dataProcessing;

import com.recSys.util.HuiMinType;
import com.recSys.util.ResourcePathHandler;

import java.io.*;

/**
 * Created by dell on 2018/1/25.
 */
public class Deal4RecCemetery {
    public static final String rootPath = ResourcePathHandler.getProgrameRootPath();
    static final String filePath4Cemes = rootPath + "\\data\\initial\\recCeme_initial.txt";
    static final String filePath4Cemes_Out = rootPath + "\\data\\format\\recCeme_format.txt";
    static final String filePath4Cemes_train = rootPath + "\\data\\trainSet\\recCeme_train.txt";
    static final String filePath4Cemes_test = rootPath + "\\data\\testSet\\recCeme_test.txt";
    public static int count = 0; //数据条数

    /**
     * 选用线性SVM算法 处理死亡人口数据 根据对“骨灰去向”的选择结果为用户分类
     *
     * 该函数将输入的非数字型数字转为数字型
     * Currently libsvm supports only numerical data. You may have to change non-numerical data to numerical.
     * For example, you can use several binary attributes to represent a categorical attribute.
     *  c_posID   1~4728
     *  c_age     4729
     *  c_sex     4730 4731  男 女
     *  d_sex     4732 4733
     *  d_age     4734
     *  d_posID   4735~9462
     *  d_HMtype  9463~9475
     *  d_Ethnic  9476~9533 民族
     *  d_Religon  9534~9540 宗教
     *  d_Funeral  9541~9663 殡仪馆
     *  d_ASHESType  骨灰去向
     */
    public static void formatDeadInfo4Cemeteries() {
        try{
            int[] hmType = new HuiMinType().getHmType();
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath4Cemes)),"UTF-8"));
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(filePath4Cemes_Out)),"UTF-8"));
            String lineTxt = null;
            while ((lineTxt = br.readLine()) != null) {
                String[] values = lineTxt.split("\t");
                bw.write(Integer.valueOf(values[10]) + " "); //d_ashesGo
                bw.write(values[0] + ":1 "); //c_posID
                bw.write("4729:" + Integer.valueOf(values[1])/100.0 + " "); //c_age
                if(values[2].equals("1"))
                    bw.write("4730:1 "); //c_sex
                else
                    bw.write("4731:1 "); //c_sex
                if(values[3].equals("1"))
                    bw.write("4732:1 "); //d_sex
                else
                    bw.write("4733:1 "); //d_sex
                bw.write("4734:" + Integer.valueOf(values[4])/100.0 + " "); //d_age
                bw.write(4734 + Integer.valueOf(values[5]) + ":1 "); //d_posID
                bw.write(9462 + hmType[Integer.valueOf(values[6])] + ":1 "); //d_HMtype
                bw.write(9475 + Integer.valueOf(values[7]) + ":1 "); //d_Ethnic
                bw.write(9534 + Integer.valueOf(values[8]) + ":1 "); //d_Religon
                bw.write(9501 + Integer.valueOf(values[9]) + ":1 "); //d_funOrgID 山东殡仪馆编号从40开始的
                bw.newLine();
                count++;
            }
            br.close();
            bw.close();
        } catch (Exception e) {
            System.err.println("read errors :" + e.getMessage());
        }
    }

    /**
     * 切分数据集为训练集和测试集
     */
    public  static void divideData() {
        try {
            int test_count = 5000;
            System.out.println("testSet count: " + test_count);
            Runtime rt = Runtime.getRuntime();
            Process pr = rt.exec("python " + rootPath + "\\tools\\subset.py " + filePath4Cemes_Out +
                    " " + test_count + " " + filePath4Cemes_test + " " + filePath4Cemes_train);
            BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream(), "GBK"));
            String line = null;
            while ((line = input.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
//        formatDeadInfo4Cemeteries();
        divideData();
    }
}
